package cn.bupt.adt.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bupt.adt.dao.autogenerate.FinanceMapper;
import cn.bupt.adt.entity.autogenerate.Finance;
import cn.bupt.adt.entity.autogenerate.FinanceExample;
import cn.bupt.adt.entity.autogenerate.OrderView;
import cn.bupt.adt.entity.autogenerate.OrderViewExample;
import cn.bupt.adt.model.FinModel;
import cn.bupt.adt.model.FinanceModel;
import cn.bupt.adt.service.IFinanceService;
import cn.bupt.adt.util.BeanUtil;
import cn.bupt.adt.util.ExportExcel;
import cn.bupt.adt.util.ExportExcelUtil;
@Service
public class FinanceService implements IFinanceService {

	@Autowired
	FinanceMapper financeMapper;

	@Override
	public List<FinanceModel> getAllFinance() {
		List<Finance> finances = financeMapper.selectByExample(new FinanceExample());
		List<FinanceModel> financeModels = new ArrayList<FinanceModel>(finances.size());
/*		for(int i = 0;i<financeModels.size();i++) {
			FinanceModel fm = new FinanceModel();
			Finance finance = finances.get(i);
			try {
				BeanUtil.fatherToChild(finance, fm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(i == 0)
				fm.setRemains(finance.getMoney());
			else {
				FinanceModel pre = financeModels.get(i-1);
				fm.setRemains(pre.getRemains()+finance.getFlag() * finance.getMoney());
			}
			financeModels.add(fm);
		}*/
		transferModel(finances, financeModels);
		return financeModels;
	}

	@Override
	public List<FinanceModel> getFinanceByDate(Date startDate, Date ednDate) {
		FinanceExample ex = new FinanceExample();
		ex.createCriteria().andTimeBetween(startDate, ednDate);
		List<Finance> finances = financeMapper.selectByExample(ex);
		List<FinanceModel> financeModels = new ArrayList<FinanceModel>(finances.size());
/*		for(int i = 0;i<financeModels.size();i++) {
			FinanceModel fm = new FinanceModel();
			Finance finance = finances.get(i);
			try {
				BeanUtil.fatherToChild(finance, fm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(i == 0)
				fm.setRemains(finance.getMoney());
			else {
				FinanceModel pre = financeModels.get(i-1);
				fm.setRemains(pre.getRemains()+finance.getFlag() * finance.getMoney());
			}
			financeModels.add(fm);
		}*/
		transferModel(finances, financeModels);
		return financeModels;
	}
	private void transferModel(List<Finance> finances,List<FinanceModel> financeModels) {
		for(int i = 0;i<finances.size();i++) {
			FinanceModel fm = new FinanceModel();
			Finance finance = finances.get(i);
			try {
				BeanUtil.fatherToChild(finance, fm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(i == 0)
				fm.setRemains(finance.getMoney()*finance.getFlag());
			else {
				FinanceModel pre = financeModels.get(i-1);
				fm.setRemains(pre.getRemains()+finance.getFlag() * finance.getMoney());
			}
			financeModels.add(fm);
		}
	}

	@Override
	public void addFinance(Finance finacne) {
		financeMapper.insert(finacne);
	}

	@Override
	public List<FinanceModel> getFinanceByDateAndName(Date startDate, Date ednDate, String name) {
		FinanceExample ex = new FinanceExample();
		ex.createCriteria().andTimeBetween(startDate, ednDate).andNameLike('%'+name+'%');
		List<Finance> finances = financeMapper.selectByExample(ex);
		List<FinanceModel> financeModels = new ArrayList<FinanceModel>(finances.size());
/*		for(int i = 0;i<financeModels.size();i++) {
			FinanceModel fm = new FinanceModel();
			Finance finance = finances.get(i);
			try {
				BeanUtil.fatherToChild(finance, fm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(i == 0)
				fm.setRemains(finance.getMoney());
			else {
				FinanceModel pre = financeModels.get(i-1);
				fm.setRemains(pre.getRemains()+finance.getFlag() * finance.getMoney());
			}
			financeModels.add(fm);
		}*/
		transferModel(finances, financeModels);
		return financeModels;
	}

	@Override
	public void exportFinanceExcel(Date startDate, Date endDate, HttpServletResponse response,String name) {
		ExportExcel<FinModel> excel = new ExportExcel<>();
		String[] headers = { "类型", "日期", "金额", "余额", "名称" };
		FinanceExample ex = new FinanceExample();
		if(name == null || "".equals(name))
			ex.createCriteria().andTimeBetween(startDate, endDate);
		else
			ex.createCriteria().andNameLike('%'+name+'%').andTimeBetween(startDate, endDate);
		List<Finance> finances = financeMapper.selectByExample(ex);

		List<FinModel> OrderExcelModel = new ArrayList<>();
		// 转变成OrderExcelModel格式的
		for(int i = 0;i<finances.size();i++) {
			FinModel fm = new FinModel();
			Finance finance = finances.get(i);
			fm.setFlag(finance.getFlag() == 1?"收入":"支出");
			fm.setName(finance.getName());
			fm.setMoney(finance.getMoney());
			fm.setTime(finance.getTime());
			if(i == 0)
				fm.setRemains(finance.getMoney()*finance.getFlag());
			else {
				FinModel pre = OrderExcelModel.get(i-1);
				fm.setRemains(pre.getRemains()+finance.getFlag() * finance.getMoney());
			}
			OrderExcelModel.add(fm);
		}
		try {
			String deskPath = ExportExcelUtil.getDeskPath();
//			System.out.println(deskPath);
			String filePath = null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
			if(startDate != null) {
				filePath = deskPath + File.separator+sdf.format(startDate) + "-" +sdf.format(endDate)+ "财务统计.xls";
			}
			else
				filePath = deskPath + File.separator +  "所有财务.xls";
			OutputStream out = new FileOutputStream(filePath);
//	        Long st1 = System.currentTimeMillis();
	        excel.exportExcel(headers, OrderExcelModel, out);
//			long end1 = System.currentTimeMillis();
//			System.out.println("导出excel耗时："+(end1-st1)/1000 +"s");
			
			out.close();
//	        Long st2 = System.currentTimeMillis();
			ExportExcelUtil.download(filePath, response);
//			long end2 = System.currentTimeMillis();
//			System.out.println("传输excel耗时："+(end2-st2)/1000 +"s");
			return ;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ;		
	}
}
